package cn.org.guhao.practice.go.thread;

import cn.org.guhao.practice.go.util.WordUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.Map;

public class CountWord implements Runnable {

  private static final Logger logger = LoggerFactory.getLogger(CountWord.class);

  private String filename;

  private int index;

  private String dir = "C:\\Users\\guhao\\Documents\\Projects\\tmp\\";

  public CountWord(String filename, int index) {
    this.filename = filename;
    this.index = index;
  }

  @Override
  public void run() {
    //开始时间
    Instant start = Instant.now();
    File file = new File(filename);
    List<String> words = null;
    try {
      words = FileUtils.readLines(file, StandardCharsets.UTF_8);
    } catch (IOException e) {
      logger.error("文件读取异常", e);
    }
    if(words == null || words.size() == 0){
      logger.info("第[{}]次读取单词数量[0]", index+1);
      return;
    }
    logger.info("第[{}]次读取单词数量[{}]", index+1, words.size());

    // 方案一
    Map<String,Long> subCountWords = WordUtils.countWords(words);
    WordUtils.addCountWord(subCountWords);
    logger.info("第[{}]次统计完成[{}]个单词", index+1,subCountWords.size());

    // 方案二
//    WordUtils.increaseCountWord(words);
//    logger.info("第[{}]次统计完成[{}]个单词", index+1,WordUtils.wordCountAtomic.size());

    // 方案三
//    WordUtils.addCountWord(words);
//    logger.info("第[{}]次统计完成[{}]个单词", index+1,WordUtils.wordCountLongAdder.size());
  }
}
